package com.zt.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zt.user.UserInfo;
import com.zt.service.UserInfoService;
import com.zt.mapper.UserInfoMapper;
import com.zt.utils.JwtService;
import com.zt.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Random;

/**
* @author 不.凌
* @description 针对表【user_info】的数据库操作Service实现
* @createDate 2024-09-03 17:37:56
*/
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
    implements UserInfoService{

    @Autowired
    private StringRedisTemplate redis;
    @Resource
    private UserInfoMapper userInfoMapper;


    @Override
    public Result login(String username, String password, String code) {
        if(StringUtils.checkValNull(username)||StringUtils.checkValNull(password)){
            return Result.error("用户名或密码不能为空");
        }
        UserInfo userInfo = this.getOne(Wrappers.<UserInfo>lambdaQuery().eq(UserInfo::getUsername, username).eq(UserInfo::getPassword, password));
        if(userInfo==null){
            return Result.error("用户不存在");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId",userInfo.getUserId());
        String token = JwtService.createToken(map);
        redis.opsForValue().set("userId", userInfo.getUserId().toString());
        return Result.success("登录成功",token);
    }

    @Override
    public Result code() {
        int code = new Random().nextInt(1000) + 9000;
        return Result.success(String.valueOf(code));
    }

    @Override
    public Result getUserInfo() {
        String userId = redis.opsForValue().get("userId");
        return Result.success("获取成功",userId);
    }

    @Override
    public String getUserById(Integer uid) {
        UserInfo userInfo = this.getOne(Wrappers.<UserInfo>lambdaQuery().eq(UserInfo::getUserId, uid));
        String name = userInfo.getName();
        return name;
    }

    @Override
    public UserInfo getUserInfoById(Integer userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        return userInfo;
    }

    @Override
    public Result getByName(String username) {
        UserInfo userInfo = userInfoMapper.selectByUsername(username);
        System.err.println("userInfo = " + userInfo);
//        Integer uid=userInfo.getUserId();
//        System.out.println("uid = " + uid);
        return Result.success(userInfo);

    }


    @Override
    public UserInfo getUserInfoUserId() {
        Integer userId = Integer.valueOf(redis.opsForValue().get("userId"));
        UserInfo one = this.getOne(Wrappers.<UserInfo>lambdaQuery().eq(userId != null, UserInfo::getUserId, userId));
        return one;
    }
    @Override
    public UserInfo getUserInfoByUserInfo(String idCard) {
        UserInfo one = this.getOne(Wrappers.<UserInfo>lambdaQuery()
                .eq(StringUtils.isNotBlank(idCard),UserInfo::getIdCard,idCard));
        return one;
    }
}




